const App =getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
      target:'',
      targetList:[],
      targetNum:0
  },
  onShareAppMessage: function () {
    // return {
    //   title: '疼痛久治不愈？还有一招',
    //   path: '/pages/navigation/index/index',
    //   imageUrl: 'https://7633-v3-92ah4-1300208657.tcb.qcloud.la/static/share_all.jpg',
    // }
  },
  onLoad(opt){
    this.setData({
      treatment_item_id:opt.treatment_item_id?opt.treatment_item_id:'',
      week:opt.week?opt.week:''
    })
    this.xGetTarget()
    this.xGetAllTarget()
  },
  xChildReload: function () {
      var pages = getCurrentPages();//当前页面栈
      if (pages.length > 1) {
        var beforePage = pages[pages.length - 2];//获取上一个页面实例对象
        beforePage.xInfo();//触发父页面中的方法
      }
  },
  xToTarget(){
    // redirectTo
    wx.navigateTo({
      url: '/subsale/pages/product/target/index?is_read='+true+'&treatment_item_id='+this.data.treatment_item_id+'&week='+this.data.week,
    })
  },
  // 用户当前目标
  xGetTarget(){ 
    let that=this;
    wx.cloud.callFunction({
      name: 'user',
      data: {
          action: "get_user_target"
      }
    }).then(res => {
      that.setData({
        target:res.result
      })
    })
  },
  // 获取用户所有目标
  xGetAllTarget(){
    let that =this 
    wx.cloud.callFunction({
      name: 'user',
      data: {
          action: "get_all_user_target"
      }
    }).then(res => {
        that.data.targetNum = 0
        for(let index in res.result){
          if(res.result[index].is_complete ==true){
            that.setData({
              targetNum:that.data.targetNum+1
            })
            res.result[index].num = that.data.targetNum
          }
        }
        that.setData({
          targetList:res.result
        })
    })
  },
  xTodel:function(e){
    let that =this;
    let id =e.currentTarget.dataset.id
    let dataObj = {
      _id:id,
      is_delete:true,
    }
    wx.showModal({
      title: '',
      content: '确定删除该已完成的里程碑吗？',
      showCancel:true,
      confirmText:'确定',
      cancelText:'再想想',
      success (res) {
        if (res.confirm) {
          wx.cloud.callFunction({
            name: 'user',
            data: {
                action: "update_user_target_status",
                dataObj:dataObj
            }
          }).then(res => {
              that.xGetAllTarget()
          })
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
   
  },
  // 完成目标
  xToComplete(){
    // redirectTo
    let dataObj = {
      _id:this.data.target._id,
      is_complete:true
    }
      wx.showModal({
          title: '确定完成当前目标?',
          content: '已完成的目标将被纳入里程碑',
          showCancel:true,
          confirmText:'是的',
          success (res) {
            if (res.confirm) {
              App.globalData.is_Refresh =true
              wx.cloud.callFunction({
                name: 'user',
                data: {
                    action: "update_user_target_status",
                    dataObj:dataObj
                }
              }).then(res => {
                wx.navigateTo({
                  url: '/subsale/pages/product/target/index?is_read=true&&is_complate=true',
                })
              })
            } else if (res.cancel) {
              console.log('用户点击取消')
            }
          }
      })
    
    
  }
})